<?php
require_once 'config.php'; 
require_once 'startup.php'; 

require_once APP_DIR . 'pagination.php';

// Check for user login
if(!$user->isLogged()){ 
    header('Location: ' . $config['url_admin'] . 'login.php');    
    exit();
}

// Render index page
$page_title = 'Havhokeren - Administration';
$title = 'Subscribers list';
$filter = false;

$logged = $user->getUserName();

$action = $config['url_admin'] . 'edit_subscriber.php';         
$insert = '#';
$delete = '#';
$edit_user = $config['url_admin'] . 'changepassword.php';
$logout = $config['url_admin'] . 'logout.php';    
$newsletter = $config['url_admin'] . 'newsletter.php';    

$text_insert = 'Insert';
$text_delete = 'Delete';
$text_edit = 'Edit';
$text_logout = 'Logout';
$text_edit_user = 'Change password';
$text_subscriber = 'Subscribers';
$text_newsletter = 'Newsletter';
$text_email = 'Email address';
$text_date = 'Date register';

$list = array();
$query = $db->query("SELECT * FROM " . DB_PREFIX . "newsletter");
foreach($query->rows as $row){
    $list[] = array(
        'email' => $row['email'],
        'date'  => strftime('%d-%m-%Y', strtotime($row['date_added'])),
        'name'  => $row['name'],
    );    
}    

$empty = 'No results!';
$query_total = $db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "newsletter");
$page = isset($request->get['page']) ? $request->get['page'] : 1;      
$pagination = new Pagination();
$pagination->total = $query_total->row['total'];
$pagination->page = $page;
$pagination->limit = $config['page_limit']; 
$pagination->url = $config['url_admin'] . 'index.php?page={page}';
$pagination = $pagination->render();

include_once 'header.php';
?>

<script type="text/javascript"><!--    
    jQuery(document).ready(function(){ 
        jQuery('#dialog').dialog({
            autoOpen: false,
            width: 300,
            modal: true,
            resizable: false,
            buttons: {
                "Cancel": function() { 
                    jQuery(this).dialog("close"); 
                },
                "Ok": function() { 
                    jQuery(this).dialog("close"); 
                    jQuery('#edit_subscriber').submit();
                }
            }
        }); 
        jQuery('#dialog1').dialog({
            autoOpen: false,
            width: 300,
            modal: true,
            resizable: false,
            buttons: {
                "Close": function() { 
                    jQuery(this).dialog("close"); 
                }
            }
        });       
                                      
        jQuery('#insert').click(function(even){
            even.preventDefault(); 
            
            jQuery('#method').val('insert'); 
            jQuery('#edit_subscriber').submit();
        });
        
        jQuery('#delete').click(function(even){
            even.preventDefault(); 
            
            var checked = 0;
            jQuery('#edit_subscriber input[name="selected[]"]').each(function(){
                if(jQuery(this).is(':checked')){
                    jQuery(this).val(jQuery(this).siblings('input[name="deleted[]"]').val());
                    checked++;
                }
            });
            
            jQuery('#method').val('delete');  
            if(checked == 0){
                jQuery('#dialog1').dialog('open');    
            }
            else{
                jQuery('#dialog').dialog('open');    
            }
        })        
        
        jQuery('#edit_subscriber a.edit').click(function(even){
            even.preventDefault(); 
            
            jQuery('#method').val('edit'); 
            jQuery('#email').val(jQuery(this).attr('rel')); 
            jQuery('#edit_subscriber').submit();
        })
    });     
//-->
</script>

<body>
    <div id="header">
        <div class="div1"><img style="cursor: pointer;" src="<?php echo $config['url_admin']; ?>image/logo.png" title="<?php echo $title; ?>" onclick="location = '<?php echo $config['url_admin']; ?>'" /></div>
        <?php if ($logged) { ?>
            <div class="div2"><img src="<?php echo $config['url_admin']; ?>image/lock.png" alt="" style="position: relative; top: 3px;" />You are logged in as: &nbsp;<?php echo $logged; ?></div>
        <?php } ?>
    </div>
    
    <?php if ($logged) { ?>
        <div id="menu">
            <ul class="nav left" style="display: block;">
                <li id="subscriber" class="selected"><a class="top" href="javascript:void(0);"><?php echo $text_subscriber; ?></a></li>
                <li id="newsletter"><a class="top" href="<?php echo $newsletter; ?>"><?php echo $text_newsletter; ?></a></li>                
            </ul>
            <ul class="nav right">                
                <li id="edit_user"><a class="top" href="<?php echo $edit_user; ?>"><?php echo $text_edit_user; ?></a></li>
                <li id="logout"><a class="top" href="<?php echo $logout; ?>"><?php echo $text_logout; ?></a></li>
            </ul>
        </div>
    <?php } ?>
    
    <div id="content">
        <div class="breadcrumb"></div>
        
        <?php if(isset($error)) { ?> 
            <div class="warning"><?php echo $error; ?></div>
        <?php } ?>
        <?php if(isset($success)){ ?>
            <div class="success"><?php echo $success; ?></div>
        <?php } ?>
    
        <div id="dialog" title="Confirm" style="display: none;">
            <p>Delete cannot be undone! Are you sure you want to do this?</p>
        </div>
        <div id="dialog1" title="Confirm" style="display: none;">
            <p>Please select an item to delete.</p>
        </div>

        <div class="box">
            <div class="left"></div>
            <div class="right"></div>
            <div class="heading">
                <h1 style="background-image: url('<?php echo $config['url_admin']; ?>image/mail.png');"><?php echo $title; ?></h1>
                <div class="buttons">
                    <a id="insert" class="button" href="#"><span><?php echo $text_insert; ?></span></a>
                    <a id="delete" class="button" href="#"><span><?php echo $text_delete; ?></span></a>
                </div>
            </div>
            
            <div class="content">
                <form id="edit_subscriber" action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
                    <table class="list">
                        <thead>
                            <tr>
                                <td width="1" style="text-align: center;"><input type="checkbox" name="selected[]" onclick="jQuery('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
                                <td class="left"><?php echo $text_email; ?></td>
                                <td class="left"><?php echo $text_date; ?></td>                            
                                <!-- td class="right"><?php //echo $actions; ?></td -->
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($filter){ ?>                                    
                                <tr class="filter">                                 
                                    <td></td>
                                    <td><input type="text" name="filter_name" value="<?php //echo $filter_email; ?>" /></td>
                                    <td><input type="text" name="filter_email" value="<?php //echo $filter_date; ?>" /></td>                                
                                    <!-- td></td -->
                                </tr>
                            <?php } ?>
                            
                            <?php if(!empty($list)){ ?>                                    
                                <?php foreach($list as $item){ ?> 
                                    <tr>
                                        <td class="select_item" style="text-align: center;">
                                            <input type="checkbox" value="1" name="selected[]" />
                                            <input type="hidden" name="deleted[]" value="<?php echo $item['email']; ?>" />
                                        </td>
                                        <td class="left"><?php echo $item['email']; ?></td>
                                        <td class="left"><?php echo $item['date']; ?></td>
                                        <!-- td class="right"><a class="edit" href="#" rel="<?php echo $item['email']; ?>"><span><?php echo $text_edit; ?></span></a></td -->                            
                                    </tr>                                     
                                <?php } ?>                       
                            <?php }else{ ?>
                                <tr>
                                    <td class="center" colspan="3"><?php echo $empty; ?></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    
                    <input type="hidden" id="method" name="method" value="" /> 
                    <input type="hidden" id="email" name="email" value="" /> 
                </form>
                <div class="pagination"><?php echo $pagination; ?></div>
            </div>
        </div>
    </div>
    
    <?php include_once 'footer.php'; ?>
</body>
</html>
